package com.ruoyi.web.controller.statistics;

import java.text.NumberFormat;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.statistics.domain.StatisticsTrain;
import com.ruoyi.statistics.service.IStatisticsTrainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.StatisticsPeople;
import com.ruoyi.statistics.service.IStatisticsPeopleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 应用人员 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-11-11
 */
@Controller
@RequestMapping("/statistics/statisticsPeople")
public class StatisticsPeopleController extends BaseController
{
    private String prefix = "statistics/statisticsPeople";
	
	@Autowired
	private IStatisticsPeopleService statisticsPeopleService;

	@Autowired
	private IStatisticsTrainService statisticsTrainService;

	@RequiresPermissions("statistics:statisticsPeople:view")
	@GetMapping()
	public String statisticsPeople(ModelMap mmap)
	{

		NumberFormat nt = NumberFormat.getPercentInstance();
		nt.setMinimumFractionDigits(2);

		int operateCount = 0;
		int operateSkill = 0;

		int explainCount = 0;
		int explainSkill = 0;

		int trainCount=0;

		String operatetotals = "";
		String operateskills = "";
		String explaintotals = "";
		String explainskills = "";

		List<StatisticsPeople> list = statisticsPeopleService.selectStatisticsPeopleList(new StatisticsPeople());
		for (int i = 0; i < list.size(); i++) {
			StatisticsPeople statisticsPeople = list.get(i);

			String operateapply =statisticsPeople.getOperateapply();
			if (StringUtils.isNotEmpty(operateapply)){
				operateCount+=Integer.parseInt(operateapply);
			}
			String operateskill =statisticsPeople.getOperateskill();
			if (StringUtils.isNotEmpty(operateskill)){
				operateSkill+=Integer.parseInt(operateskill);
			}

			String explainapply =statisticsPeople.getExplainapply();
			if (StringUtils.isNotEmpty(explainapply)){
				explainCount+=Integer.parseInt(explainapply);
			}
			String explainskill =statisticsPeople.getExplainskill();
			if (StringUtils.isNotEmpty(explainskill)){
				explainSkill+=Integer.parseInt(explainskill);
			}

			String trainpeople =statisticsPeople.getTrainpeople();
			if (StringUtils.isNotEmpty(trainpeople)){
				trainCount+=Integer.parseInt(trainpeople);
			}

			operatetotals = operatetotals + statisticsPeople.getOperatetotal() + ",";
			operateskills = operateskills + statisticsPeople.getOperateskill() + ",";
			explaintotals = explaintotals + statisticsPeople.getExplaintotal() + ",";
			explainskills = explainskills + statisticsPeople.getExplainskill() + ",";

		}
		mmap.put("operateapply",operateCount);
		mmap.put("operateskill",operateSkill);
		float operaterate = (float)operateSkill / (float)operateCount;
		mmap.put("operaterate", nt.format(operaterate));

		float explainrate = (float)explainSkill / (float)explainCount;
		mmap.put("explainapply", explainCount);
		mmap.put("explainskill", explainSkill);
		mmap.put("explainrate", nt.format(explainrate));

		float trainrate = (float)trainCount / (float)(operateCount + explainCount);
		mmap.put("trainrate", nt.format(trainrate));

		operatetotals = operatetotals.substring(0, operatetotals.length() - 1);
		operateskills = operateskills.substring(0, operateskills.length() - 1);
		explaintotals = explaintotals.substring(0, explaintotals.length() - 1);
		explainskills = explainskills.substring(0, explainskills.length() - 1);
		mmap.put("operatetotals", operatetotals);
		mmap.put("operateskills", operateskills);
		mmap.put("explaintotals", explaintotals);
		mmap.put("explainskills", explainskills);

		List<StatisticsTrain> trainList = statisticsTrainService.selectStatisticsTrainList(new StatisticsTrain());

		int daqing_count = 0;int daqing_xueyuan = 0;int daqing_teacher = 0;int liaohe_count = 0;int liaohe_xueyuan = 0;int liaohe_teacher = 0;
		int changqing_count = 0;int changqing_xueyuan = 0;int changqing_teacher = 0;int talimu_count = 0;int talimu_xueyuan = 0;int talimu_teacher = 0;
		int xinjiang_count = 0;int xinjiang_xueyuan = 0;int xinjiang_teacher = 0;int xinan_count = 0;int xinan_xueyuan = 0;int xinan_teacher = 0;
		int jilin_count = 0;int jilin_xueyuan = 0;int jilin_teacher = 0;int dagang_count = 0;int dagang_xueyuan = 0;int dagang_teacher = 0;
		int qinghai_count = 0;int qinghai_xueyuan = 0;int qinghai_teacher = 0;int huabei_count = 0;int huabei_xueyuan = 0;int huabei_teacher = 0;
		int tuha_count = 0;int tuha_xueyuan = 0;int tuha_teacher = 0;int jidong_count = 0;int jidong_xueyuan = 0;int jidong_teacher = 0;
		int yumen_count = 0;int yumen_xueyuan = 0;int yumen_teacher = 0;int meicengqi_count = 0;int meicengqi_xueyuan = 0;int meicengqi_teacher = 0;
		int nanfang_count = 0;int nanfang_xueyuan = 0;int nanfang_teacher = 0;int kantanbeijing_count = 0;int kantanbeijing_xueyuan = 0;int kantanbeijing_teacher = 0;
		int zhejiang_count = 0;int zhejiang_xueyuan = 0;int zhejiang_teacher = 0;int kantanxibei_count = 0;int kantanxibei_xueyuan = 0;int kantanxibei_teacher = 0;
		int hanzhoudizhi_count = 0;int hanzhoudizhi_xueyuan = 0;int hanzhoudizhi_teacher = 0;


		for (int j = 0; j < trainList.size(); j++) {
			StatisticsTrain statisticsTrain = trainList.get(j);

			if (statisticsTrain.getUnitid().equals("1"))
			{
				daqing_count++;
				daqing_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				daqing_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("2"))
			{
				liaohe_count++;
				liaohe_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				liaohe_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("3"))
			{
				changqing_count++;
				changqing_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				changqing_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("4"))
			{
				talimu_count++;
				talimu_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				talimu_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("5"))
			{
				xinjiang_count++;
				xinjiang_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				xinjiang_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("6"))
			{
				xinan_count++;
				xinan_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				xinan_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("7"))
			{
				jilin_count++;
				jilin_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				jilin_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("8"))
			{
				dagang_count++;
				dagang_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				dagang_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("9"))
			{
				qinghai_count++;
				qinghai_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				qinghai_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("10"))
			{
				huabei_count++;
				huabei_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				huabei_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("11"))
			{
				tuha_count++;
				tuha_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				tuha_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("12"))
			{
				jidong_count++;
				jidong_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				jidong_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("13"))
			{
				yumen_count++;
				yumen_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				yumen_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("14"))
			{
				zhejiang_count++;
				zhejiang_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				zhejiang_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("15"))
			{
				meicengqi_count++;
				meicengqi_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				meicengqi_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("16"))
			{
				nanfang_count++;
				nanfang_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				nanfang_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("17"))
			{
				kantanbeijing_count++;
				kantanbeijing_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				kantanbeijing_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("18"))
			{
				kantanxibei_count++;
				kantanxibei_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				kantanxibei_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
			else if (statisticsTrain.getUnitid().equals("19"))
			{
				hanzhoudizhi_count++;
				hanzhoudizhi_xueyuan += Integer.parseInt(statisticsTrain.getRealcount());
				hanzhoudizhi_teacher += Integer.parseInt(statisticsTrain.getTeachercount());
			}
		}

		mmap.put("traincounts", daqing_count + "," + liaohe_count + "," + changqing_count + "," + talimu_count + "," + xinjiang_count + "," + xinan_count + "," +
				jilin_count + "," + dagang_count + "," + qinghai_count + "," + huabei_count + "," + tuha_count + "," + jidong_count + "," + yumen_count + "," +
				zhejiang_count + "," + meicengqi_count + "," + nanfang_count + "," + kantanbeijing_count + "," + kantanxibei_count + "," + hanzhoudizhi_count);
		mmap.put("trainpeoples", daqing_xueyuan + "," + liaohe_xueyuan + "," + changqing_xueyuan + "," + talimu_xueyuan + "," + xinjiang_xueyuan + "," + xinan_xueyuan + "," +
				jilin_xueyuan + "," + dagang_xueyuan + "," + qinghai_xueyuan + "," + huabei_xueyuan + "," + tuha_xueyuan + "," + jidong_xueyuan + "," + yumen_xueyuan + "," +
				zhejiang_xueyuan + "," + meicengqi_xueyuan + "," + nanfang_xueyuan + "," + kantanbeijing_xueyuan + "," + kantanxibei_xueyuan + "," + hanzhoudizhi_xueyuan);
		mmap.put("trainteachers", daqing_teacher + "," + liaohe_teacher + "," + changqing_teacher + "," + talimu_teacher + "," + xinjiang_teacher + "," + xinan_teacher + "," +
				jilin_teacher + "," + dagang_teacher + "," + qinghai_teacher + "," + huabei_teacher + "," + tuha_teacher + "," + jidong_teacher + "," + yumen_teacher + "," +
				zhejiang_teacher + "," + meicengqi_teacher + "," + nanfang_teacher + "," + kantanbeijing_teacher + "," + kantanxibei_teacher + "," + hanzhoudizhi_teacher);

		return prefix + "/statisticsPeople";
	}
	
	/**
	 * 查询应用人员列表
	 */
	@RequiresPermissions("statistics:statisticsPeople:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(StatisticsPeople statisticsPeople)
	{
		startPage();
        List<StatisticsPeople> list = statisticsPeopleService.selectStatisticsPeopleList(statisticsPeople);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出应用人员列表
	 */
	@RequiresPermissions("statistics:statisticsPeople:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StatisticsPeople statisticsPeople)
    {
    	List<StatisticsPeople> list = statisticsPeopleService.selectStatisticsPeopleList(statisticsPeople);
        ExcelUtil<StatisticsPeople> util = new ExcelUtil<StatisticsPeople>(StatisticsPeople.class);
        return util.exportExcel(list, "statisticsPeople");
    }
	
	/**
	 * 新增应用人员
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存应用人员
	 */
	@RequiresPermissions("statistics:statisticsPeople:add")
	@Log(title = "应用人员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(StatisticsPeople statisticsPeople)
	{		
		return toAjax(statisticsPeopleService.insertStatisticsPeople(statisticsPeople));
	}

	/**
	 * 修改应用人员
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		StatisticsPeople statisticsPeople = statisticsPeopleService.selectStatisticsPeopleById(id);
		mmap.put("statisticsPeople", statisticsPeople);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存应用人员
	 */
	@RequiresPermissions("statistics:statisticsPeople:edit")
	@Log(title = "应用人员", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(StatisticsPeople statisticsPeople)
	{		
		return toAjax(statisticsPeopleService.updateStatisticsPeople(statisticsPeople));
	}
	
	/**
	 * 删除应用人员
	 */
	@RequiresPermissions("statistics:statisticsPeople:remove")
	@Log(title = "应用人员", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(statisticsPeopleService.deleteStatisticsPeopleByIds(ids));
	}
	
}
